/* 

This do file computes regressions with crop shocks to see if the treatments had any effect on risk sharing 
Only on WG and Control clusters and includes standard errors computed using a wild bootstrap.
Controls included for seasonality and changes in household size

*/


clear all
cap log close
set more off
set matsize 1600
set mem 100m
version 11.1

global ROOT ""
global data "$ROOT/Data"
global dofiles "$ROOT/DoFiles"
global tables "$ROOT/Tables"
global figures "$ROOT/Figures"

u "$data\analysisfinal.dta", clear

*-------------------------------------------------------------------
*Deciding on the zones and villages
*-------------------------------------------------------------------
global ourcluster "zone_o"

*Village variable for the village fixed effects
local village "vill1"
 
/* First, generating interaction dummies between the shocks and women's group treatment variable */
qui {

*Shock variables
foreach y of varlist dcrop dintensity {
gen `y'_treat1 = `y'*wg_o

}

gen control = wg_o ==0
replace control = . if wg_o==.

foreach y of varlist dcrop dintensity {
gen `y'_control = `y'*control

}
}
*closing quietly above

*--------------------------------------------------------
*Building some variables that were not created before
*--------------------------------------------------------
gen d12_18=dfem12_18+dmale12_18
gen dmt18=dfemmt18+dmalemt18


/* Next for the regression. The regression we run is as follows:
dln(civt) = beta*dshock(ivt) + beta2*dshock(ivt)*wg_o + vt + uivt
It comes from the model of perfect risk sharing with CRRA utility. Test is similar to that used by Mace (1991) and Cochrane (1991)
 */



********************************************
 * Specification with village-time dummies
*********************************************
 
global regressors1 " ddays_oct dlt6 dsix_12 d12_18 dmt18"

gen dtot_food=dmfoodc
gen dltot_food=dlmfoodc

cd "$tables"
*---------------------------------------------------------------
*Consumption smoothing specification: total consumption and food
*----------------------------------------------------------------
cap erase "cons_smooth.log"
 
*******************
*Total Consumption
*******************
global outregopts "nocons br dec(4)"

set more off
*Changes in Log Consumption - Shock binary
areg dltot_cons dcrop dcrop_treat1  $regressors1, absorb(vill1) cluster ($ourcluster)
loneway dltot_cons zone_o
outreg2 using "cons_smooth.log", $outregopts  ctitle("dltot_cons") keep(dcrop dcrop_treat1)
areg dltot_cons dcrop dcrop_treat1  $regressors1, absorb(vill1) cluster ($ourcluster)
boottest dcrop 
boottest dcrop_treat1

*Changes in Consumption - Loss as fraction of predicted consumption new
areg dltot_cons dintensity dintensity_treat1  $regressors1, absorb(vill1) cluster ($ourcluster)
loneway dltot_cons zone_o
outreg2 using "cons_smooth.log", $outregopts  ctitle("dltot_cons") keep(dintensity dintensity_treat1)
areg dltot_cons dintensity dintensity_treat1  $regressors1, absorb(vill1) cluster ($ourcluster)
boottest dintensity 
boottest dintensity_treat1



cap erase "food_smooth.log"
*******************
*Food Consumption
*******************
*Changes in Log Consumption - Shock binary
areg dltot_food dcrop dcrop_treat1  $regressors1, absorb(vill1) cluster ($ourcluster)
loneway dltot_food zone_o
outreg2 using "cons_smooth.log", $outregopts  ctitle("dltot_food") keep(dcrop dcrop_treat1)
areg dltot_food dcrop dcrop_treat1  $regressors1, absorb(vill1) cluster ($ourcluster)
boottest dcrop 
boottest dcrop_treat1

*Changes in Consumption - Loss as fraction of predicted consumption new
areg dltot_food dintensity dintensity_treat1  $regressors1, absorb(vill1) cluster ($ourcluster)
loneway dltot_food zone_o
outreg2 using "cons_smooth.log", $outregopts  ctitle("dltot_food") keep(dintensity dintensity_treat1)
areg dltot_food dintensity dintensity_treat1  $regressors1, absorb(vill1) cluster ($ourcluster)
boottest dintensity 
boottest dintensity_treat1

/* Figures
*/
//Define the matrix in which to save coefficients and then the confidence intervals
matrix coeff = J(3,4,.)
matrix colnames coeff = "Total consumption, incidence" "Total consumption, intensity" "Food consumption, incidence" "Food consumption, intensity"
matrix CI1 = J(2,4,.)
matrix colnames CI1 = "Total consumption, incidence" "Total consumption, intensity" "Food consumption, incidence" "Food consumption, intensity"
matrix rownames CI1 = ll95_shock ul95_shock 
matrix CI2=J(2,4,.)
matrix colnames CI2 = "Total consumption, incidence" "Total consumption, intensity" "Food consumption, incidence" "Food consumption, intensity"
matrix rownames CI2 = ll95_shock ul95_shock
matrix CI3=J(2,4,.)
matrix colnames CI2 = "Total consumption, incidence" "Total consumption, intensity" "Food consumption, incidence" "Food consumption, intensity"
matrix rownames CI2 = ll95_shock ul95_shock

areg dltot_cons dcrop dcrop_treat1  $regressors1, absorb(vill1) cluster($ourcluster)
matrix coeff[1,1]=e(b)[1,1]
matrix coeff[2,1]=e(b)[1,2]
boottest dcrop , boot(wild) seed(10101) bootcl($ourcluster)
matrix CI1[1,1]=r(CI)[1,1]
matrix CI1[2,1]=r(CI)[1,2]
boottest dcrop_treat1 , boot(wild) seed(10101) bootcl($ourcluster)
matrix CI2[1,1]=r(CI)[1,1]
matrix CI2[2,1]=r(CI)[1,2]

qui areg dltot_cons dcrop_control dcrop_treat1  $regressors1, absorb(vill1) cluster ($ourcluster)
matrix coeff[3,1]=e(b)[1,2]
boottest dcrop_treat1 , boot(wild) seed(10101) bootcl($ourcluster)
matrix CI3[1,1]=r(CI)[1,1]
matrix CI3[2,1]=r(CI)[1,2]

areg dltot_cons dshare_new dshare_new_treat1  $regressors1, absorb(vill1) cluster($ourcluster)
matrix coeff[1,2]=e(b)[1,1]
matrix coeff[2,2]=e(b)[1,2]
boottest dshare_new , boot(wild) seed(10101) bootcl($ourcluster)
matrix CI1[1,2]=r(CI)[1,1]
matrix CI1[2,2]=r(CI)[1,2]
boottest dshare_new_treat1 , boot(wild) seed(10101) bootcl($ourcluster)
matrix CI2[1,2]=r(CI)[1,1]
matrix CI2[2,2]=r(CI)[1,2]

qui areg dltot_cons dshare_new_control dshare_new_treat1  $regressors1, absorb(vill1) cluster ($ourcluster)
matrix coeff[3,2]=e(b)[1,2]
boottest dshare_new_treat1 , boot(wild) seed(10101) bootcl($ourcluster)
matrix CI3[1,2]=r(CI)[1,1]
matrix CI3[2,2]=r(CI)[1,2]

//dltot_food

areg dltot_food dcrop dcrop_treat1  $regressors1, absorb(vill1) cluster($ourcluster)
matrix coeff[1,3]=e(b)[1,1]
matrix coeff[2,3]=e(b)[1,2]
boottest dcrop , boot(wild) seed(10101) bootcl($ourcluster)
matrix CI1[1,3]=r(CI)[1,1]
matrix CI1[2,3]=r(CI)[1,2]
boottest dcrop_treat1 , boot(wild) seed(10101) bootcl($ourcluster)
matrix CI2[1,3]=r(CI)[1,1]
matrix CI2[2,3]=r(CI)[1,2]

qui areg dltot_food dcrop_control dcrop_treat1  $regressors1, absorb(vill1) cluster ($ourcluster)
matrix coeff[3,3]=e(b)[1,2]
boottest dcrop_treat1 , boot(wild) seed(10101) bootcl($ourcluster)
matrix CI3[1,3]=r(CI)[1,1]
matrix CI3[2,3]=r(CI)[1,2]

areg dltot_food dshare_new dshare_new_treat1  $regressors1, absorb(vill1) cluster($ourcluster)
matrix coeff[1,4]=e(b)[1,1]
matrix coeff[2,4]=e(b)[1,2]
boottest dshare_new , boot(wild) seed(10101) bootcl($ourcluster)
matrix CI1[1,4]=r(CI)[1,1]
matrix CI1[2,4]=r(CI)[1,2]
boottest dshare_new_treat1 , boot(wild) seed(10101) bootcl($ourcluster)
matrix CI2[1,4]=r(CI)[1,1]
matrix CI2[2,4]=r(CI)[1,2]

qui areg dltot_food dshare_new_control dshare_new_treat1  $regressors1, absorb(vill1) cluster ($ourcluster)
matrix coeff[3,4]=e(b)[1,2]
boottest dshare_new_treat1 , boot(wild) seed(10101) bootcl($ourcluster)
matrix CI3[1,4]=r(CI)[1,1]
matrix CI3[2,4]=r(CI)[1,2]

*recast(bar) barwidt(0.3)

set scheme s1mono

coefplot (matrix(coeff[1,]), ci(CI1) label(Control)) (matrix(coeff[3,]), ci(CI3) label(Treatment)) (matrix(coeff[2,]), ci(CI2) label("Treatment Effect")), xline(0)  ciopts(recast(rcap)) citop 
graph save "$figures/main_results", replace
graph export "$figures/main_results.pdf", replace
